package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Dept;
import com.neuedu.entity.Doctor;
import com.neuedu.entity.Level;
import com.neuedu.entity.Schedule;
import com.neuedu.mapper.DeptMapper;
import com.neuedu.mapper.DoctorMapper;
import com.neuedu.mapper.LevelMapper;
import com.neuedu.mapper.ScheduleMapper;
import com.neuedu.service.DoctorService;
import com.neuedu.vo.DoctorCondition;
import com.neuedu.vo.DoctorRegCondition;
import com.neuedu.vo.DoctorVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class DoctorServiceImpl extends ServiceImpl<DoctorMapper, Doctor> implements DoctorService {

    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private DeptMapper deptMapper;

    @Resource
    private LevelMapper levelMapper;

    @Resource
    private ScheduleMapper scheduleMapper;

    @Override
    public Page<DoctorVO> getDoctorList(DoctorCondition doctorCondition) {

        Page<Doctor> doctorPage = new Page(doctorCondition.getCurrentPage(), doctorCondition.getPageSize());

        QueryWrapper<Doctor> wrapper = new QueryWrapper<>();

        wrapper.like("doctor_name", doctorCondition.getDoctorName());

        if(doctorCondition.getDoctorType()!=null){
            wrapper.eq("doctor_type", doctorCondition.getDoctorType());
        }

        if(doctorCondition.getDeptId()!=null){
            wrapper.eq("dept_id", doctorCondition.getDeptId());
        }

        if(doctorCondition.getLevelId()!=null){
            wrapper.eq("level_id", doctorCondition.getLevelId());
        }

        doctorPage = doctorMapper.selectPage(doctorPage, wrapper);

        //创建目标分页对象
        Page<DoctorVO> doctorVOPage = new Page<>();

        //填充目标分页对象
        BeanUtils.copyProperties(doctorPage, doctorVOPage);

        //创建DoctorVO列表
        List<DoctorVO> doctorVOList = new ArrayList<>();

        for (Doctor doctor : doctorPage.getRecords()) {

            DoctorVO doctorVO = new DoctorVO();

            BeanUtils.copyProperties(doctor, doctorVO);

            //填充科室名称
            Dept dept = deptMapper.selectById(doctor.getDeptId());
            if(dept!=null) {
                doctorVO.setDeptName(dept.getDeptName());
            }else{
                doctorVO.setDeptName("");
            }

            //填充级别名称
            Level level = levelMapper.selectById(doctor.getLevelId());
            if(level!=null) {
                doctorVO.setLevelName(level.getLevelName());
            }else{
                doctorVO.setLevelName("");
            }

            //填充排班名称
            Schedule schedule = scheduleMapper.selectById(doctor.getScheduleId());
            if(schedule!=null) {
                doctorVO.setScheduleName(schedule.getScheduleName());
            }else{
                doctorVO.setScheduleName("");
            }

            System.out.println(doctorVO);

            //添加到VO列表中
            doctorVOList.add(doctorVO);
        }

        doctorVOPage.setRecords(doctorVOList);

        //返回目标分页对象
        return doctorVOPage;
    }

    @Override
    public boolean removeEnable(int doctorId) {
        return doctorMapper.findRegisterCount(doctorId)==0;
    }

    @Override
    public boolean deleteBatch(String doctorIds) {

        //将字符串拆分成数组  "1,2,3" --> [1,2,3]
        String[] arr = doctorIds.split(",");

        //字符串数组转换集合  [1,2,3] --> {1,2,3}
        List<Integer> list = new ArrayList<>();

        for (String s : arr) {
            list.add(Integer.parseInt(s));
        }

        return this.removeByIds(list);
    }

    @Override
    public Doctor login(Doctor doctor) {
       /* return this.query()
                .eq("doctor_name", doctor.getDoctorName())
                .eq("password", doctor.getPassword())
                .one();*/

        List<Doctor> list = this.query()
                .eq("doctor_name", doctor.getDoctorName())
                .eq("password", doctor.getPassword())
                .list();

        if(list.size() > 0) {
            return list.get(0);
        }else{
            return null;
        }

    }

    @Override
    public List<Doctor> getDoctorList(DoctorRegCondition doctorRegCondition) {

        //步骤1---先根据科室和挂号级别查询
        List<Doctor> doctorList = this.query()
                .eq("dept_id", doctorRegCondition.getDeptId())
                .eq("level_id", doctorRegCondition.getLevelId())
                .list();

        //步骤2---将看诊日期字符串转换成日期型
        Date regDate = null;
        try {
            regDate = new SimpleDateFormat("yyyy-MM-dd").parse(doctorRegCondition.getRegisterDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //步骤3---根据看诊日期和看诊午别，确定排班规则的索引  getDay()返回0-6  10001111100101   index表示在排班规则中的位置序号
        int index = regDate.getDay() * 2  + doctorRegCondition.getRegisterNoon();

        System.out.println("遍历前=" + doctorList);

        //使用迭代器遍历集合，删除不上班的医生
        Iterator<Doctor> it = doctorList.iterator();

        while(it.hasNext()){

            //获取每一个医生
            Doctor doctor = it.next();

            //查询医生对应的排班信息
            Schedule schedule = scheduleMapper.selectById(doctor.getScheduleId());

            //获取排班规则字符串
            String scheduleRule = schedule.getScheduleRule();

            if(scheduleRule.charAt(index)=='0'){    //不上班

                System.out.println("删除不上班的" + doctor);

                //通过迭代器，删除集合元素
                it.remove();
            }

        }

       /* //使用普通循环遍历集合时，不要删除元素，否则会抛出java.util.ConcurrentModificationException异常
        for (Doctor doctor : doctorList) {

             //获取医生对应的排班信息
             Schedule schedule = scheduleMapper.selectById(doctor.getScheduleId());

             String scheduleRule = schedule.getScheduleRule();

             if(scheduleRule.charAt(index)=='0'){    //不上班

                 System.out.println("删除不上班的" + doctor);

                 //从列表中删除元素
                 doctorList.remove(doctor);
             }
        }*/

        System.out.println("遍历后=" + doctorList);

        return doctorList;
    }


}
